﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Syspetro.Core.DynamicApi
{
    public class GroupExtraInfo
    {
        public string Group { get; set; }

        public string Version { get; set; }
        private string TitleV { get; set; }

        public string Title
        {
            get
            {
                if (string.IsNullOrEmpty(TitleV))
                    return $"{Group}_{Version}";
                else
                    return TitleV;
            }
            set
            {
                TitleV = value;
            }
        }
    }
    public static class DynamicApiGroups
    {
        /// <summary>
        /// 所有的接口方法
        /// </summary>
        public static List<MethodInfo> ActionTypes = new();
        /// <summary>
        /// 所有控制器类型
        /// </summary>
        public static List<Type> ControllerTypes = new();
        /// <summary>
        /// 分组信息
        /// </summary>
        public static List<GroupExtraInfo> GroupExtraInfos = new();
        /// <summary>
        /// 缓存
        /// </summary>
        private static readonly Dictionary<MethodInfo, List<GroupExtraInfo>> GroupCache = new();

        public static void AddGroupInfo(GroupExtraInfo info)
        {
            if (!GroupExtraInfos.Where(t => t.Group == info.Group).Any())
                GroupExtraInfos.Add(info);
        }

        public static List<GroupExtraInfo> GetActionGroups(MethodInfo method)
        {
            if (GroupCache.ContainsKey(method))
                return GroupCache[method];
            var groupExtras = new List<GroupExtraInfo>();
            var group1 = ResolveGroupExtraInfo(method);
            if (group1 != null)
                groupExtras.Add(group1);
            var group2 = ResolveGroupExtraInfo(method.ReflectedType);
            if (group2 != null)
                groupExtras.Add(group2);
            if (groupExtras.Count == 0)
                groupExtras = GroupExtraInfos;

            GroupCache.Add(method, groupExtras);
            return groupExtras;
        }
        private static GroupExtraInfo ResolveGroupExtraInfo(MethodInfo method)
        {
            var apiDynamicSettings = method.GetCustomAttribute<DynamicApiAttribute>(true);
            if (!string.IsNullOrEmpty(apiDynamicSettings?.GroupName))
                return GroupExtraInfos.Where(t => t.Group == apiDynamicSettings.GroupName).FirstOrDefault();
            var apiExplorerSettings = method.GetCustomAttribute<ApiExplorerSettingsAttribute>(true);
            if (!string.IsNullOrEmpty(apiExplorerSettings?.GroupName))
                return GroupExtraInfos.Where(t => t.Group == apiExplorerSettings.GroupName).FirstOrDefault();
            return null;
        }
        private static GroupExtraInfo ResolveGroupExtraInfo(Type method)
        {
            var apiDynamicSettings = method.GetCustomAttribute<DynamicApiAttribute>(true);
            if (!string.IsNullOrEmpty(apiDynamicSettings?.GroupName))
                return GroupExtraInfos.Where(t => t.Group == apiDynamicSettings.GroupName).FirstOrDefault();
            var apiExplorerSettings = method.GetCustomAttribute<ApiExplorerSettingsAttribute>(true);
            if (!string.IsNullOrEmpty(apiExplorerSettings?.GroupName))
                return GroupExtraInfos.Where(t => t.Group == apiExplorerSettings.GroupName).FirstOrDefault();
            return null;
        }
    }
}
